import { observable, action } from "mobx"
import { getUserId } from '@/utils/utils';
import { network } from "@/utils/network"
import Taro from '@tarojs/taro'


class RateDataModel {

  @observable
  grade: number

  @observable
  content: string

  @observable
  order_id: string

  @observable.ref
  list: any

  @observable.ref
  rateList: Array<string>

  constructor() {
    this.grade = 5;
    this.content = '非常满意，无可挑剔'
    this.order_id = ''
    this.list = []
    this.rateList = [
      '非常差',
      '差',
      '一般',
      '好',
      '非常好'
    ]
  }


  @action
  setValue(key, value) {
    this[key] = value;
  }



  async getData() {
    const { data } = await network.get('/xxm/rate/list', {
      user_id: getUserId()
    });
    this.list = data.list.filter(it => it.order !== null);
  }


  async creteRate() {

    if (!this.content) {
      Taro.showToast({ icon: 'none', title: '评价内容不能为空' });
      return;
    }

    const { code } = await network.post('/xxm/rate/create', {
      user_id: getUserId(),
      grade: this.grade,
      content: this.content,
      order_id: this.order_id
    });

    if (code === 0) {
      Taro.showToast({ icon: 'success', title: '评价成功' })
      setTimeout(() => {
        Taro.navigateTo({ url: `/pages/center/orders/index?status=RATE` });
      }, 700);

    }
  }


}

export const RateModel = new RateDataModel()
